package com.winter.collection.List;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


public class ArrayListToArray {

    @Test
    public void toArray() {
        List list = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
        //抛出异常，返回的是Object[]，向下转型后出现异常
        Integer[] array = (Integer[]) list.toArray();
        System.out.println(array);
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
        Integer[] integers = toArray1(list);
        Integer[] integers1 = toArray2(list);
        Integer[] integers2 = toArray3(list);
        System.out.println(integers);
        System.out.println(integers1);
        System.out.println(integers2);

    }

    public static Integer[] toArray1(ArrayList<Integer> list) {
        Integer[] integers = new Integer[list.size()];
        list.toArray(integers);
        return integers;
    }

    public static Integer[] toArray2(ArrayList<Integer> list) {
        Integer[] integers = list.toArray(new Integer[0]);
        return integers;
    }

    public static Integer[] toArray3(ArrayList<Integer> list) {
        Integer[] integers = new Integer[list.size()];
        Integer[] integers1 = list.toArray(integers);
        return integers1;
    }


}
